/**
 * action creator 所有请求的创建
 * @author wangjiang
 * 2018-06-12 13:42:34
 */

 
import {
    LOGIN,
} from './ActionTypes'

import NavigationService from '@/services/NavigationService'
import UserApi from '@/api/user'

/**
 * 登录
 * @param {*} payload 
 * @param {*} routeTo 登录成功后的跳转页面
 * @param {*} routeToParams 登录成功后的跳转参数
 */

export const login  =  ( payload, suceessRoute = 'Home', suceessRouteParams = {} ) => {
    return async (dispatch) => {
        try {
            dispatch( { type: LOGIN.LOGIN_REQUEST }) // 发送请求
            let ret  = await UserApi.login(payload)
            dispatch({type: LOGIN.LOGIN_SUCCESS,  token: ret.access_token})
            //登录成功之后的跳转
            NavigationService.navigate(suceessRoute, suceessRouteParams)
        } catch ( e ) {
            console.log(e)
            dispatch({ type: LOGIN.LOGIN_ERROR, e})// 请求失败
        }
        
    }
}
